using System.Collections.ObjectModel;
using System.Net;
using System.Text;
using System.Windows.Input;

namespace SteamInstaller.ViewModel
{
    public class GameViewModel : ViewModelBase
    {
        public ObservableCollection<GameInfo> GameList { get; set; }

        private string _accountId;
        public string AccountId
        {
            get { return _accountId; }
            set
            {
                if (_accountId != value)
                {
                    _accountId = value;
                    RaisePropertyChanged(() => AccountId);
                }
            }
        }

        public ICommand LoadGamesCommand { get; set; }

        public GameViewModel()
        {
            GameList = new ObservableCollection<GameInfo>();
            WireCommands();
        }

        private void WireCommands()
        {
            LoadGamesCommand = new DelegateCommand(LoadGameList);
        }

        private void LoadGameList(object obj)
        {
            HttpWebRequest httpWebRequest = WebRequest.Create("http://steamcommunity.com/id/" + AccountId + "/games") as HttpWebRequest;
            if (httpWebRequest == null) return;

            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;

            StringBuilder builder = new StringBuilder();
            byte[] buf = new byte[8192];
            int count = 0;
            do
            {
                if (httpWebResponse != null)
                {
                    using (var stream = httpWebResponse.GetResponseStream())
                    {
                        if (stream != null) count = stream.Read(buf, 0, count);
                        if (count != 0) builder.Append(Encoding.ASCII.GetString(buf, 0, count));
                    }
                }
            } while (count > 0);

            ParsePage(builder.ToString());
        }

        private void ParsePage(string steamPage)
        {
            
        }
    }
}
